<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/permission/permission' }">权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加权限</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <div style="width: 30%;">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      </div>
      <div style="width: 50%;">
      <el-form-item label="请求路径值" prop="value">
        <el-input v-model="ruleForm.value"></el-input>
      </el-form-item>
      </div>
      <div style="width: 70%;">
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      </div>
      <div style="width: 20%;">
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      </div>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="$router.push('/permission/permission')">返回列表</el-button>
      </el-form-item>
    </el-form>


  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name: '测试数据001',
        value:'qwe',
        description: '测试数据的简介001',
        sort: '99',
      },
    rules: {
      name: [
        {required: true, message: '请输入权限名称', trigger: 'blur'},
        {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
      ],
      value: [
        {required: true, message: '请输入请求路径值', trigger: 'blur'},
        {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
      ],
      description: [
        {required: true, message: '请输入权限描述', trigger: 'blur'}
      ],
      sort: [
        {required: true, message: '请输入排序序号', trigger: 'blur'},
        {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/,message: '排序序号必须是0-99',trigger: 'blur'}
      ]
    }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9201/permission/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);
          let localJwt = localStorage.getItem('localJwt');
          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt
                }
              })
              .post(url,formData ).then((response) =>{
            let jsonResult = response.data;
            if (jsonResult.state == 20000){
              this.$message({
                message: '添加权限成功！',
                type: 'success'
              });
              this.resetForm(formName);
            }else {
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
            }

          });

        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },

}
</script>

